package cn.gson.fengboot.model.service;

import cn.gson.fengboot.controller.DbusinessController;
import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.DneedMapper;
import cn.gson.fengboot.model.mapper.DschemeMapper;
import cn.gson.fengboot.model.mapper.DvieMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DschemeService {


    @Autowired
    DschemeMapper dschemeMapper;

    @Autowired
    DschemeDao dschemeDao;

    @Autowired
    DbusinessDao dbusinessDao;

    @Autowired
    DphaseDao dphaseDao;

    @Autowired
    DneedMapper dneedMapper;

    @Autowired
    DvieDao dvieDao;

    @Autowired
    DvieMapper dvieMapper;
    @Autowired
    DbusinessController controller;

    public List<Dscheme> selectscheme(Integer busId) {
        return dschemeMapper.selectscheme(busId);
    }

    public void addscheme(Dscheme dscheme) {
        dschemeDao.save(dscheme);
        Dbusiness dbusiness = dbusinessDao.findById(dscheme.getBusId().getBusId()).get();
        Integer  phaseId = dbusiness.getPhase().getPhaseId();
        if(phaseId<3){
            Dphase dphase1 = dphaseDao.findById(phaseId + 1).get();
            dbusiness.setBusPossibility(dphase1.getPhasePoss());
            dbusiness.setPhase(dphase1);
        }

    }

    public List<Object> selectBytype(String type, Integer busId, HttpServletRequest request) {
        String getbuspeople = controller.getbuspeople(request);
        List<Object> objects1 = null;
        if(type.equals("客户需求")){
            List<Dneed> byBusId = dneedMapper.findByBusId(busId);
            objects1 = Arrays.asList( byBusId.toArray() ) ;
        }
        if(type.equals("竞争对手")){
            List<Dbusvie> byBusId = dvieMapper.selectmyvie(busId,getbuspeople);
            objects1 = Arrays.asList( byBusId.toArray() ) ;
        }
        System.out.println(objects1);
        return objects1;
    }

    public Dscheme verifymyscheme(String schemeTheme,HttpServletRequest request) {
        String getbuspeople = controller.getbuspeople(request);
        return dschemeDao.findBySchemeTheme(schemeTheme,getbuspeople);
    }
}

